<?php 
/**
 * 公用控制器
 */
class CommonController extends Controller{
	/**
	 * 调用顶级分类,为了方便各个控制器调用分配顶级分类
	 */
	protected function topCate(){
		$count = M('hd_ask')->field("count(*)")->all();
		$count = $count[0]['count(*)'];
		$topCate = M('hd_category')->where('pid=0')->all();
		$this->assign('count',$count);
		$this->assign('topCate',$topCate);
	}
	protected function rightCate(){
		if(isset($_SESSION['uid'])){
			$rightCate = M('hd_user')->where("uid={$_SESSION['uid']}")->find();
			if($rightCate['answer']){
				$adopt = ($rightCate['accept']/$rightCate['answer'])*100;
				$adopt = round($adopt);
			}else{
				$adopt = 0;
			}
			$this->assign('adopt', $adopt);
			$this->assign('rightCate',$rightCate);
		}
		/*
		 * 历史回答数最多资料
		 */
		$mostAnswer = M('hd_user')->order("answer DESC")->limit(1)->find();
		$mostAnswerAcc = ($mostAnswer['accept']/$mostAnswer['answer'])*100;
		$mostAnswerAcc = round($mostAnswerAcc);
		$this->assign('mostAnswerAcc', $mostAnswerAcc);
		$this->assign('mostAnswer', $mostAnswer);
		/**
		 * 采纳数前三名，帮助过的人数前三名
		 */
		 $mostAdopt = M('hd_user')->order("accept DESC")->field("username,accept")->limit(3)->all();
		 $this->assign('mostAdopt', $mostAdopt);
		/**
		 * 今日回答最多的人
		 */
//		 获取今天0，0，0分的时间戳
		 $date = strtotime(date('Y-m-d'));
		 $array = M('hd_answer')->where("time>$date")->field("uid")->order("count(*) DESC")->limit(1)->group("uid")->find();
		 if(!empty($array)){
		 	$todayMostUid = $array['uid'];
			$todayMostAns = M('hd_user')->where("uid={$todayMostUid}")->field("username,accept,answer")->find();
			$todayMostAns['acc'] = round(($todayMostAns['accept']/$todayMostAns['answer'])*100);
		    $this->assign('todayMostAns', $todayMostAns);
		 }	
	}
	protected function leftCate(){
		$leftCate = M('hd_user')->where("uid={$_SESSION['uid']}")->find();
		if($leftCate['answer']){
			$adopt = ($leftCate['accept']/$leftCate['answer'])*100;
			$adopt = round($adopt);
		}else{
			$adopt = 0;
		}
		$leftCate['adopt'] = $adopt;
		$this->assign('leftCate',$leftCate);
		
		$a = isset($_GET['a'])?$_GET['a']:'index';
		$this->assign('a',$a);
	}	 
}


